local poyuan = fk.CreateSkill {

  name = "joy__poyuan",

  tags = {  },

}



poyuan:addEffect(fk.GameStart, {
  name = "joy__poyuan",
  anim_type = "control",
  events = {fk.GameStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(poyuan.name) and (event == fk.GameStart or (event == fk.TurnStart and target == player)) then
      if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
        return table.find(player.room:getOtherPlayers(player), function(p) return not p:isNude() end)
      else
        local catapult = table.find(U.prepareDeriveCards(player.room, poyuan_catapult, "joy__poyuan_catapult"), function (id)
          return player.room:getCardArea(id) == Card.Void
        end)
        return catapult and U.canMoveCardIntoEquip(player, catapult)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
      local targets = table.filter(room:getOtherPlayers(player), function(p) return not p:isNude() end)
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#joy__poyuan-choose", poyuan.name, true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return room:askForSkillInvoke(player, poyuan.name, nil, "#joy__poyuan-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
      local to = room:getPlayerById(event:getCostData(self))
      local cards = room:askForCardsChosen(player, to, 1, 2, "he", poyuan.name)
      room:throwCard(cards, poyuan.name, to, player)
    else
      local catapult = table.find(U.prepareDeriveCards(room, poyuan_catapult, "joy__poyuan_catapult"), function (id)
        return player.room:getCardArea(id) == Card.Void
      end)
      if catapult then
        room:setCardMark(Fk:getCardById(catapult), MarkEnum.DestructOutMyEquip, 1)
        U.moveCardIntoEquip (room, player, catapult, poyuan.name, true, player)
      end
    end
  end,
})
poyuan:addEffect(fk.TurnStart, {
  name = "joy__poyuan",
  anim_type = "control",
  events = {fk.GameStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(poyuan.name) and (event == fk.GameStart or (event == fk.TurnStart and target == player)) then
      if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
        return table.find(player.room:getOtherPlayers(player), function(p) return not p:isNude() end)
      else
        local catapult = table.find(U.prepareDeriveCards(player.room, poyuan_catapult, "joy__poyuan_catapult"), function (id)
          return player.room:getCardArea(id) == Card.Void
        end)
        return catapult and U.canMoveCardIntoEquip(player, catapult)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
      local targets = table.filter(room:getOtherPlayers(player), function(p) return not p:isNude() end)
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#joy__poyuan-choose", poyuan.name, true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return room:askForSkillInvoke(player, poyuan.name, nil, "#joy__poyuan-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if table.find(player:getEquipments(Card.SubtypeOffensiveRide), function(id) return Fk:getCardById(id).name == "joy__catapult" end) then
      local to = room:getPlayerById(event:getCostData(self))
      local cards = room:askForCardsChosen(player, to, 1, 2, "he", poyuan.name)
      room:throwCard(cards, poyuan.name, to, player)
    else
      local catapult = table.find(U.prepareDeriveCards(room, poyuan_catapult, "joy__poyuan_catapult"), function (id)
        return player.room:getCardArea(id) == Card.Void
      end)
      if catapult then
        room:setCardMark(Fk:getCardById(catapult), MarkEnum.DestructOutMyEquip, 1)
        U.moveCardIntoEquip (room, player, catapult, poyuan.name, true, player)
      end
    end
  end,
})

return poyuan